package chances.epg.data.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.api.tools.ContentTool;
import chances.epg.data.model.ContentVO;
import chances.epg.entity.content.AbstractContent;

public class ContentConvertTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3785334819623413193L;
	private Object content;
	private String var;
	
	@Override
	public int doEndTag() throws JspException {
		if(content instanceof AbstractContent){
			AbstractContent ct = (AbstractContent)content;
			ContentVO vo = new ContentVO();
			if(StringUtils.isNotBlank(ct.getContentType())){
				String type = ContentTool.getInstance().typeName(ct.getContentType());
				//ct.setContentType(type);
				vo.setContentType(type);
			}
			
			if(StringUtils.isNotBlank(ct.getContentBaseTags())){
				String baseTags = ContentTool.getInstance().baseTagNames(ct.getContentBaseTags());
				//ct.setContentBaseTags(baseTags);
				vo.setContentBaseTags(baseTags);
			}
			
			if(StringUtils.isNotBlank(ct.getContentOpTags())){
				String opTags = ContentTool.getInstance().opTagNames(ct.getContentOpTags());
				//ct.setContentOpTags(opTags);
				vo.setContentOpTags(opTags);
			}
			pageContext.setAttribute(this.var, vo);
		}
		
		return super.doEndTag();
	}

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
	
}
